package praktikumid.p05_e;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author ago.luberg
 *
 */
public class ReadFromWeb {
	/**
	 * kommentaar.
	 */
	public static final String GROUPS_URL = "https://groups.google.com/group/iti0011/feed/rss_v2_0_msgs.xml";

	/**
	 * kommentaar.
	 */
	public static final String GTRANSLATE_URL = "http://translate.google.com/m?hl=en&sl=et&tl=XX&ie=UTF-8&prev=_m&q=";
	/**
	 * @param args Käsurea parameetrid
	 */
	public static void main(String[] args) {
		/*
		 * Järgmine kord:
		 * args, faili kirjutamine/lugemine
		 */
		
		/*
		 * 
		 * Mida teha?
		 * 
		 * käsurea parameetrid args
		 *  java -jar Feed.jar -h
		 *  java -jar Feed.jar ru desc 7
		 *  java -jar Feed.jar desc ru
		 *  
		 *  java -jar Feed.jar --sort=desc --lang=ru --cache-size=7
		 * 
		 * tõmbame RSS xml-i alla
		 * otsime üles postituse
		 *   <item></item> täägide vahel
		 * otsime postitusest tääge:
		 *   <title></title>
		 *   <pubDate></pubDate>
		 *   <description></description>
		 * parsime:
		 *   indexOf
		 *   substring
		 *   charAt
		 *   trim
		 *   jm sõbrad
		 * iga item ära parsida
		 * HTML ära parsida
		 * parsitud tulemused listi/arraysse/vms panna
		 * 
		 * saadate GTranslate'i title ja description
		 * saadud vastus läheb listi/arraysse/vms
		 *   optional: prindite välja.
		 */
		
		String content = readGoogleGroups();
		
		// otsime üles <item></item>
		
		// String.indexOf tagastab esimese positsiooni stringis
		int itemStartPos = content.indexOf("<item>");
		int itemEndPos = content.indexOf("</item>");
		System.out.println(itemStartPos);
		System.out.println(itemEndPos);
		
		String item = content.substring(itemStartPos, itemEndPos);
		System.out.println(item);
		
		// otsime sealt üles <title></title>
		int titleStartPos = item.indexOf("<title>");
		titleStartPos = titleStartPos + "<title>".length();
		int titleEndPos = item.indexOf("</title>");
		String title = item.substring(titleStartPos, titleEndPos);
		System.out.println(title);
		
		// TODO: tee meetodiks!
		
		// <br> == &lt;br&gt;
		// <description><description></description>
		// <description>&lt;description&gt;</description>

		
		// tõlgime
		String t = translate(title, "zh-TW");
		System.out.println(t);
		
	}
	
	/**
	 * Let's open Google Groups URL and
	 * read all the contents into a string.
	 * @return Whole xml.
	 */
	public static String readGoogleGroups() {
		try {
			URL url = new URL(GROUPS_URL);
			URLConnection c = url.openConnection();
			c.setRequestProperty("User-agent", "Mozilla/5.0");
			BufferedReader br = new BufferedReader(
					new InputStreamReader(
							c.getInputStream())
					);
			String content = "";
			String line;
			while ((line = br.readLine()) != null) {
				//System.out.println(line);
				content += line + "\n";
			}
			return content;

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Translates string s into language.
	 * @param s String to translate
	 * @param language Language used for translating
	 * @return translated string
	 */
	public static String translate(String s, String language) {
		
		try {
			s = s.replace(" ", "+");
			String u = GTRANSLATE_URL + s;
			u = u.replace("tl=XX", "tl=" + language);
			URL url = new URL(u);
			URLConnection c = url.openConnection();
			c.setRequestProperty("User-agent", "Mozilla/5.0");
			BufferedReader br = new BufferedReader(
					new InputStreamReader(
							c.getInputStream())
					);
			String content = "";
			String line;
			while ((line = br.readLine()) != null) {
				//System.out.println(line);
				content += line + "\n";
			}
			
			//System.out.println(content);
			
			// otsime üles tõlke
			String start = "<div dir=\"ltr\" class=\"t0\">";
			int itemStartPos = content.indexOf(start);
			itemStartPos += start.length();
			int itemEndPos = content.indexOf("</div>", itemStartPos);
			//System.out.println(itemStartPos);
			//System.out.println(itemEndPos);
			
			String item = content.substring(itemStartPos, itemEndPos);
			//System.out.println(item);
			
			return item;

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
